//
//  ProfileViewController.m
//  Futures
//
//  Created by Cheng on 2017/6/20.
//  Copyright © 2017年 Cheng. All rights reserved.
//

#import "ProfileViewController.h"
#import "MineViewModel.h"
#import "AccountInfo.h"
#define kGetAccountInfo @"kGetAccountInfo"
@interface ProfileViewController ()

@property (weak, nonatomic) IBOutlet UIView *header;

@property (weak, nonatomic) IBOutlet UILabel *availableFund;
@property (weak, nonatomic) IBOutlet UILabel *freezeFund;

@end

@implementation ProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initailzation];
    
    [self refreshData];
}

- (void)initailzation{
    self.navigationItem.title = @"我的";
    
    self.header.height = 160;
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTitle:@"客服" icon:@"customerservice" target:self action:@selector(clickCustomerService)];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTitle:@"设置" target:self action:@selector(clickSetting)];
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    self.navigationController.navigationBar.barTintColor = UICOLOR_RGB_Alpha(0x202020, 1);
    //去除导航栏下方的横线
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init]
                                                  forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];
}

- (void)clickCustomerService{
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel:13692227717"]]];
    [self.view addSubview:callWebview];
}

- (void)clickSetting{
    [self performSegueWithIdentifier:@"SettingTableViewController" sender:nil];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    showAlert(@"暂未开放");
}

- (void)refreshData{
    [MineViewModel requestAccountInfoWithBlock:^(id obj) {
        AccountInfo *accountInfo = obj;
        self.availableFund.text = [@"￥" stringByAppendingString: NSStringFromFloat(accountInfo.AvailableFund)];
        self.freezeFund.text = [@"￥" stringByAppendingString:NSStringFromFloat(accountInfo.Equity)];
        self.navigationItem.title = accountInfo.Name;
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    __weak typeof(self) weakSelf = self;
    [[JX_GCDTimerManager sharedInstance] scheduledDispatchTimerWithName:kGetAccountInfo timeInterval:5.0f queue:nil repeats:YES actionOption:AbandonPreviousAction action:^{
        
        [weakSelf refreshData];
    }];
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    if ([[JX_GCDTimerManager sharedInstance]existTimer:kGetAccountInfo]) {
        [[JX_GCDTimerManager sharedInstance] cancelTimerWithName:kGetAccountInfo];
    }
}

@end
